<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;
use think\Config;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        $this->view->assign('title', __('首页'));

        $jigou_list = Db::name('jigouliebiao')->limit(6)->order('weigh desc')->select();
        $this->view->assign('jigou_list', $jigou_list);

        //轮播图
        $banner_type_id = Db::name('bannertype')->where(['type' => 0, 'name' => '首页'])->column('id');
        $rs_banner = Db::name('banner')->where([
            'type_data' => 0,
            'bannertype_id' => $banner_type_id[0],
            /* 'endtime'=>['>',time()], */
        ])->order('weigh desc')->select();
        $this->assign('rs_banner', $rs_banner);

        //行业信息
        $hangyexinxi_banner = Db::name('hangyexinxi')->where(['image' => ['<>', '']])->order('weigh desc')->limit(3)->select();
        $this->assign('hangyexinxi_banner', $hangyexinxi_banner);
        $hangyexinxitype = Db::name('hangyexinxitype')->where([
            'show_switch' => 1,
            'pcindex_switch' => 1,
        ])->order('weigh desc')->limit(3)->select();
        foreach ($hangyexinxitype as $k => $v) {
            $hangyexinxitype[$k]['list'] = Db::name('hangyexinxi')->where(['type_id' => $v['id']])->order('weigh desc')->limit(4)->select();
        }
        $this->assign('hangyexinxitype', $hangyexinxitype);

        $hangyexinxi_xinwenjujiao = Db::name('hangyexinxi')->order('weigh desc')->limit(4)->select();
        $this->assign('hangyexinxi_xinwenjujiao', $hangyexinxi_xinwenjujiao);
        //上部广告位
        $adv_shangbu = Db::name('guanggao')->where(['guanggaotype_id' => 6, 'type_data' => '0'])->order('weigh desc')->limit(3)->select();
        $this->assign('adv_shangbu', $adv_shangbu);

        $adv_zhongbu = Db::name('guanggao')->where(['guanggaotype_id' => 7, 'type_data' => '0'])->order('weigh desc')->limit(3)->select();
        $this->assign('adv_zhongbu', $adv_zhongbu);

        $product_category = Db::name('category')->where(['pid' => 0, 'type' => 'product', 'name' => ['<>', '']])->where('status','normal')->select();
        $youzhi_product = [];
        $chuangxin_product = [];
        foreach ($product_category as $k => $v) {
            // 调用函数获取指定父类的所有子类ID
            // 要获取子类ID的父类ID
            $allChildIds = getChildIds($v['id']);
            array_push($allChildIds, $v['id']);
            $product_category[$k]['ids'] = $allChildIds;
            $youzhi_product[$v['id']] = Db::name('product')->alias('p')->join('qiye q', 'p.qiye_id=q.id', 'LEFT')->field('p.*,q.name as qiye_name')->where(['p.category_id' => ['in', $allChildIds]])->where(['p.status' => '2', 'biaoji' => '1'])->limit(10)->select();
            $chuangxin_product[$v['id']] = Db::name('product')->alias('p')->join('qiye q', 'p.qiye_id=q.id', 'LEFT')->field('p.*,q.name as qiye_name')->where(['p.category_id' => ['in', $allChildIds]])->where(['p.status' => '2', 'biaoji' => '2'])->limit(10)->select();

        }
        $this->assign('product_category', $product_category);
        $this->assign('youzhi_product', $youzhi_product);
        $this->assign('chuangxin_product', $chuangxin_product);

        //期刊
        $rs_qikan = Db::name('qikan')->where('show_switch', 1)->order('weigh desc,updatetime desc')->limit(15)->select();
        foreach ($rs_qikan as $k => $v) {
            $rs_qikan[$k]['type'] = 'qikan';
        }
        if (count($rs_qikan) <= 3) {
            $rs_shuji = Db::name('shuji')->order('weigh desc')->limit(2)->select();
            foreach ($rs_shuji as $k => $v) {
                $rs_shuji[$k]['type'] = 'shuji';
            }
            $rs_qikan = array_merge($rs_qikan, $rs_shuji);
        }
        $this->assign('rs_qikan', $rs_qikan);

        //人物专栏
        $rs_renwu = Db::name('renwuzhuanlantype')->where(['pcindex_switch' => 1, 'show_switch' => 1])
            ->order('weigh desc')
            ->limit(4)->select();
        foreach ($rs_renwu as $k => $v) {
            $rs_renwu[$k]['list'] = Db::name('renwuzhuanlan')->where(['renwuzhuanlantype_id' => $v['id']])
                ->limit(4)
                ->order('nianfen_date desc')
                ->order('updatetime desc')
                ->select();
        }
        $this->assign('rs_renwu', $rs_renwu);

        //行业展会
        $rs_hangyezhanhui = Db::name('hangyezhanhuitype')->where(['pcindex_switch' => 1, 'show_switch' => 1])->order('weigh desc')->limit(2)->select();
        foreach ($rs_hangyezhanhui as $k => $v) {
            $rs_hangyezhanhui[$k]['list'] = Db::name('hangyezhanhui')->where(['hangyezhanhuitype_id' => $v['id']])->limit(4)->order('weigh desc')->select();
        }
        $this->assign('rs_hangyezhanhui', $rs_hangyezhanhui);

        //下部广告
        $adv_xiabu1 = Db::name('guanggao')->where(['guanggaotype_id' => 8, 'type_data' => '0'])->order('weigh desc')->limit(2)->select();
        $adv_xiabu2 = Db::name('guanggao')->where(['guanggaotype_id' => 9, 'type_data' => '0'])->order('weigh desc')->limit(4)->select();
        $this->assign('adv_xiabu1', $adv_xiabu1);
        $this->assign('adv_xiabu2', $adv_xiabu2);

        //企业展联
        $rs_qiyelianzhan = Db::name('qiyelianzhan')->order('weigh desc')->select();
        $this->assign('rs_qiyelianzhan', $rs_qiyelianzhan);

        //友情链接
        $rs_friendlink = Db::name('friendlink')->order('weigh desc')->select();
        $this->assign('rs_friendlink', $rs_friendlink);

        return $this->view->fetch();
    }

    public function search(Request $request)
    {
        $keyword = $request->param('keyword/s', '');
        $type = $request->param('type/s', '');

        $this->assign('keyword', $keyword);
        $this->assign('type', $type);

        switch ($type) {
            case 'hangyexinxi':
                $list = Db::name('hangyexinxi')->field('a.*,t.name as type_name')->alias('a')->join('hangyexinxitype t', 't.id=a.type_id', 'LEFT')->where(['a.title' => ['like', "%$keyword%"]])->paginate(15);
                break;
            case 'biaozhunbianzhi':
                $list = Db::name('biaozhunbianzhi')->field('a.*,t.name as type_name')->alias('a')->join('biaozhuntype t', 't.id=a.biaozhuntype_id', 'LEFT')->where(['a.title' => ['like', "%$keyword%"]])->paginate(15);

                break;
            case 'huiyipeixun':
                $list = Db::name('huiyipeixun')->field('a.*,t.name as type_name')->alias('a')->join('huiyipeixuntype t', 't.id=a.huiyipeixuntype_id', 'LEFT')->where(['a.name' => ['like', "%$keyword%"]])->paginate(15);

                break;
            case 'hangyezhanhui':
                $list = Db::name('hangyezhanhui')->field('a.*,t.name as type_name')->alias('a')->join('hangyezhanhuitype t', 't.id=a.hangyezhanhuitype_id', 'LEFT')->where(['a.name' => ['like', "%$keyword%"]])->paginate(15);
                break;
            case 'renwuzhuanlan':
                $list = Db::name('renwuzhuanlan')->field('a.*,t.name as type_name')->alias('a')->join('renwuzhuanlantype t', 't.id=a.renwuzhuanlantype_id', 'LEFT')->where(['a.title' => ['like', "%$keyword%"]])->paginate(15);

                break;

            case 'qiye':
                $list = Db::name('qiye')->where(['name' => ['like', "%$keyword%"]])->paginate(15);
                break;
            case 'product':
                $list = Db::name('product')->field('p.*,q.name as qiye_name')->alias('p')
                    ->join('qiye q', 'q.id=p.qiye_id', 'LEFT')
                    ->where(['p.name' => ['like', "%$keyword%"]])
                    ->paginate(15);
 //                   ->each(function ($item, $key) {
//                    $tags=explode(',',$item['tags']);
//					if(in_array('创新',$tags)){
//						$item['tag']='chuangxin';
//					}else if(in_array('优质',$tags)){
//						$item['tag']='youzhi';
//					}else{
//						$item['tag']='';
//					}
//					return $item;
 //                   });
                break;
            case 'shukan':
                $list = Db::name('qikanwenzhang')->field('a.*,t.name as type_name')->alias('a')->join('qikanwenzhangtype t', 't.id=a.qikanwenzhangtype_id', 'LEFT')->where(['a.title' => ['like', "%$keyword%"]])->order('qikanwenzhangtype_id desc')->paginate(15);

                break;
            default:
                $list = Db::name('hangyexinxi')->field('a.*,t.name as type_name')->alias('a')->join('hangyexinxitype t', 't.id=a.type_id', 'LEFT')->where(['a.title' => ['like', "%$keyword%"]])->paginate(15);

                break;
        }

        $this->assign('list', $list);

        $this->view->assign('title', __('搜索结果'));
        return $this->view->fetch();
    }

    public function zhengshu(Request $request)
    {

        $banner = Db::name('banner')->where([
            'type_data' => 0,
            'bannertype_id' => 18
        ])->select();
        $this->assign('banner', $banner);

        $hangyexinxi_type2 = Db::name('hangyexinxitype')->where(['id' => ['>', 1], 'show_switch' => 1])->order('weigh desc')->limit(3)->select();
        foreach ($hangyexinxi_type2 as $k => $v) {
            $hangyexinxi_type2[$k]['list'] = Db::name('hangyexinxi')->where(['type_id' => $v['id']])->order('weigh desc')->limit(4)->select();
        }
        $this->assign('hangyexinxi_type2', $hangyexinxi_type2);

        $type = $request->param('type/d', 0);
        $this->assign('type', $type);

        $keyword = trim($request->param('keyword/s', ''));
        $rs = '';
        if ($keyword != '') {
            switch ($type) {
                case 0:
                    $rs = Db::name('zhengshujishurending')->where(['wanchengdanwei|rendingbianhao|jishumingcheng' => ['like', "%$keyword%"]])->find();
                    break;
                case 1:
                    $rs = Db::name('zhengshujinengpeixun')->where(['zhengshubianhao|shenfenzhenghao' => ['like', "%$keyword%"]])->find();
                    break;
            }

        }
        $this->assign('rs', $rs);

        $this->assign('xingbie_arr', [0 => '男', 1 => '女']);


        $this->view->assign('title', __('证书查询'));
        return $this->view->fetch();
    }

    public function single(Request $request)
    {
        $article = $request->param('article/s', '');
        $site = Config::get("site");
        switch ($article) {
            case 'aboutus':
                $title = '关于我们';
                $content = $site['aboutus'];
                break;
            case 'guangxuanhezuo':
                $title = '广宣合作';
                $content = $site['guangxuanhezuo'];
                break;
            case 'wangzhanfuwu':
                $title = '网站服务';
                $content = $site['wangzhanfuwu'];
                break;
            case 'falvshengming':
                $title = '法律声明';
                $content = $site['falvshengming'];
                break;
            default:
                $title = '关于我们';
                $content = $site['aboutus'];
                break;


        }
        $this->assign('title', $title);
        $this->assign('content', $content);
        return $this->view->fetch();
    }

    public function test()
    {
        echo md5('123456' . '1504846536');
    }
}
